package com.lqk.test;

/**
 * @author liqiankun
 * @date 2022/10/31 17:33
 * @description demo01使用synchronized锁实现
 **/
public class Demo1Sync {
    /**
     * 变量
     */
    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public static void main(String[] args) {
        Demo1Sync demo = new Demo1Sync();
        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (demo) {
                    /*
                        每个线程: 存一个变量, 过一会儿, 取出这个变量
                     */
                        demo.setContent(Thread.currentThread().getName() + "的数据");
                        System.out.println("-------------------------");
                        System.out.println(Thread.currentThread().getName() + "---->" + demo.getContent());
                    }
                }
            });
            // 线程0~4
            thread.setName("线程"+i);
            thread.start();
        }
    }
}
